package javax.mail.internet;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

public class ParameterList {
    private static boolean applehack;
    private static boolean decodeParameters;
    private static boolean decodeParametersStrict;
    private static boolean encodeParameters;
    private static final char[] hex = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    private String lastName;
    private Map list;
    private Set multisegmentNames;
    private Map slist;

    private static class MultiValue extends ArrayList {
        String value;

        private MultiValue() {
        }
    }

    private static class ToStringBuffer {
        private StringBuffer sb = new StringBuffer();
        private int used;

        public ToStringBuffer(int used) {
            this.used = used;
        }

        public void addNV(String name, String value) {
            value = ParameterList.quote(value);
            this.sb.append("; ");
            this.used += 2;
            if (this.used + ((name.length() + value.length()) + 1) > 76) {
                this.sb.append("\r\n\t");
                this.used = 8;
            }
            this.sb.append(name).append('=');
            this.used += name.length() + 1;
            if (this.used + value.length() > 76) {
                String s = MimeUtility.fold(this.used, value);
                this.sb.append(s);
                int lastlf = s.lastIndexOf(10);
                if (lastlf >= 0) {
                    this.used += (s.length() - lastlf) - 1;
                    return;
                } else {
                    this.used += s.length();
                    return;
                }
            }
            this.sb.append(value);
            this.used += value.length();
        }

        public String toString() {
            return this.sb.toString();
        }
    }

    private static class Value {
        String charset;
        String encodedValue;
        String value;

        private Value() {
        }
    }

    static {
        boolean z = true;
        encodeParameters = false;
        decodeParameters = false;
        decodeParametersStrict = false;
        applehack = false;
        try {
            String s = System.getProperty("mail.mime.encodeparameters");
            boolean z2 = s != null && s.equalsIgnoreCase("true");
            encodeParameters = z2;
            s = System.getProperty("mail.mime.decodeparameters");
            if (s == null || !s.equalsIgnoreCase("true")) {
                z2 = false;
            } else {
                z2 = true;
            }
            decodeParameters = z2;
            s = System.getProperty("mail.mime.decodeparameters.strict");
            if (s == null || !s.equalsIgnoreCase("true")) {
                z2 = false;
            } else {
                z2 = true;
            }
            decodeParametersStrict = z2;
            s = System.getProperty("mail.mime.applefilenames");
            if (s == null || !s.equalsIgnoreCase("true")) {
                z = false;
            }
            applehack = z;
        } catch (SecurityException e) {
        }
    }

    public ParameterList() {
        this.list = new LinkedHashMap();
        this.lastName = null;
        if (decodeParameters) {
            this.multisegmentNames = new HashSet();
            this.slist = new HashMap();
        }
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public ParameterList(java.lang.String r11) throws javax.mail.internet.ParseException {
        /*
        r10 = this;
        r9 = -4;
        r8 = -1;
        r10.<init>();
        r0 = new javax.mail.internet.HeaderTokenizer;
        r6 = "()<>@,;:\\\"\t []/?=";
        r0.<init>(r11, r6);
    L_0x000c:
        r3 = r0.next();
        r4 = r3.getType();
        if (r4 != r9) goto L_0x001f;
    L_0x0016:
        r6 = decodeParameters;
        if (r6 == 0) goto L_0x001e;
    L_0x001a:
        r6 = 0;
        r10.combineMultisegmentNames(r6);
    L_0x001e:
        return;
    L_0x001f:
        r6 = (char) r4;
        r7 = 59;
        if (r6 != r7) goto L_0x00cb;
    L_0x0024:
        r3 = r0.next();
        r6 = r3.getType();
        if (r6 == r9) goto L_0x0016;
    L_0x002e:
        r6 = r3.getType();
        if (r6 == r8) goto L_0x0053;
    L_0x0034:
        r6 = new javax.mail.internet.ParseException;
        r7 = new java.lang.StringBuilder;
        r8 = "Expected parameter name, got \"";
        r7.<init>(r8);
        r8 = r3.getValue();
        r7 = r7.append(r8);
        r8 = "\"";
        r7 = r7.append(r8);
        r7 = r7.toString();
        r6.<init>(r7);
        throw r6;
    L_0x0053:
        r6 = r3.getValue();
        r7 = java.util.Locale.ENGLISH;
        r2 = r6.toLowerCase(r7);
        r3 = r0.next();
        r6 = r3.getType();
        r6 = (char) r6;
        r7 = 61;
        if (r6 == r7) goto L_0x0089;
    L_0x006a:
        r6 = new javax.mail.internet.ParseException;
        r7 = new java.lang.StringBuilder;
        r8 = "Expected '=', got \"";
        r7.<init>(r8);
        r8 = r3.getValue();
        r7 = r7.append(r8);
        r8 = "\"";
        r7 = r7.append(r8);
        r7 = r7.toString();
        r6.<init>(r7);
        throw r6;
    L_0x0089:
        r3 = r0.next();
        r4 = r3.getType();
        if (r4 == r8) goto L_0x00b5;
    L_0x0093:
        r6 = -2;
        if (r4 == r6) goto L_0x00b5;
    L_0x0096:
        r6 = new javax.mail.internet.ParseException;
        r7 = new java.lang.StringBuilder;
        r8 = "Expected parameter value, got \"";
        r7.<init>(r8);
        r8 = r3.getValue();
        r7 = r7.append(r8);
        r8 = "\"";
        r7 = r7.append(r8);
        r7 = r7.toString();
        r6.<init>(r7);
        throw r6;
    L_0x00b5:
        r5 = r3.getValue();
        r10.lastName = r2;
        r6 = decodeParameters;
        if (r6 == 0) goto L_0x00c4;
    L_0x00bf:
        r10.putEncodedName(r2, r5);
        goto L_0x000c;
    L_0x00c4:
        r6 = r10.list;
        r6.put(r2, r5);
        goto L_0x000c;
    L_0x00cb:
        r6 = applehack;
        if (r6 == 0) goto L_0x0117;
    L_0x00cf:
        if (r4 != r8) goto L_0x0117;
    L_0x00d1:
        r6 = r10.lastName;
        if (r6 == 0) goto L_0x0117;
    L_0x00d5:
        r6 = r10.lastName;
        r7 = "name";
        r6 = r6.equals(r7);
        if (r6 != 0) goto L_0x00e9;
    L_0x00df:
        r6 = r10.lastName;
        r7 = "filename";
        r6 = r6.equals(r7);
        if (r6 == 0) goto L_0x0117;
    L_0x00e9:
        r6 = r10.list;
        r7 = r10.lastName;
        r1 = r6.get(r7);
        r1 = (java.lang.String) r1;
        r6 = new java.lang.StringBuilder;
        r7 = java.lang.String.valueOf(r1);
        r6.<init>(r7);
        r7 = " ";
        r6 = r6.append(r7);
        r7 = r3.getValue();
        r6 = r6.append(r7);
        r5 = r6.toString();
        r6 = r10.list;
        r7 = r10.lastName;
        r6.put(r7, r5);
        goto L_0x000c;
    L_0x0117:
        r6 = new javax.mail.internet.ParseException;
        r7 = new java.lang.StringBuilder;
        r8 = "Expected ';', got \"";
        r7.<init>(r8);
        r8 = r3.getValue();
        r7 = r7.append(r8);
        r8 = "\"";
        r7 = r7.append(r8);
        r7 = r7.toString();
        r6.<init>(r7);
        throw r6;
        */
        throw new UnsupportedOperationException("Method not decompiled: javax.mail.internet.ParameterList.<init>(java.lang.String):void");
    }

    private void putEncodedName(String name, String value) throws ParseException {
        int star = name.indexOf(42);
        if (star < 0) {
            this.list.put(name, value);
        } else if (star == name.length() - 1) {
            this.list.put(name.substring(0, star), decodeValue(value));
        } else {
            Object v;
            String rname = name.substring(0, star);
            this.multisegmentNames.add(rname);
            this.list.put(rname, "");
            if (name.endsWith("*")) {
                v = new Value();
                ((Value) v).encodedValue = value;
                ((Value) v).value = value;
                name = name.substring(0, name.length() - 1);
            } else {
                String v2 = value;
            }
            this.slist.put(name, v);
        }
    }

    private void combineMultisegmentNames(boolean keepConsistentOnFailure) throws ParseException {
        Object v;
        Value vv;
        for (String name : this.multisegmentNames) {
            StringBuffer sb = new StringBuffer();
            MultiValue mv = new MultiValue();
            int segment = 0;
            String charset = null;
            while (true) {
                String sname = new StringBuilder(String.valueOf(name)).append("*").append(segment).toString();
                v = this.slist.get(sname);
                if (v == null) {
                    break;
                }
                String charset2;
                mv.add(v);
                String str = null;
                if (v instanceof Value) {
                    try {
                        vv = (Value) v;
                        String evalue = vv.encodedValue;
                        str = evalue;
                        String value;
                        if (segment == 0) {
                            vnew = decodeValue(evalue);
                            charset2 = vnew.charset;
                            vv.charset = charset2;
                            try {
                                value = vnew.value;
                                vv.value = value;
                                str = value;
                            } catch (NumberFormatException e) {
                                NumberFormatException nex = e;
                                if (decodeParametersStrict) {
                                    throw new ParseException(nex.toString());
                                }
                                sb.append(str);
                                this.slist.remove(sname);
                                segment++;
                                charset = charset2;
                            } catch (UnsupportedEncodingException e2) {
                                uex = e2;
                                if (decodeParametersStrict) {
                                    throw new ParseException(uex.toString());
                                }
                                sb.append(str);
                                this.slist.remove(sname);
                                segment++;
                                charset = charset2;
                            } catch (StringIndexOutOfBoundsException e3) {
                                ex = e3;
                                if (decodeParametersStrict) {
                                    throw new ParseException(ex.toString());
                                }
                                sb.append(str);
                                this.slist.remove(sname);
                                segment++;
                                charset = charset2;
                            }
                        } else if (charset == null) {
                            this.multisegmentNames.remove(name);
                            break;
                        } else {
                            value = decodeBytes(evalue, charset);
                            vv.value = value;
                            str = value;
                            charset2 = charset;
                        }
                    } catch (NumberFormatException e4) {
                        nex = e4;
                        charset2 = charset;
                    } catch (UnsupportedEncodingException e5) {
                        uex = e5;
                        charset2 = charset;
                        if (decodeParametersStrict) {
                            UnsupportedEncodingException uex;
                            throw new ParseException(uex.toString());
                        }
                        sb.append(str);
                        this.slist.remove(sname);
                        segment++;
                        charset = charset2;
                    } catch (StringIndexOutOfBoundsException e6) {
                        ex = e6;
                        charset2 = charset;
                        if (decodeParametersStrict) {
                            StringIndexOutOfBoundsException ex;
                            throw new ParseException(ex.toString());
                        }
                        sb.append(str);
                        this.slist.remove(sname);
                        segment++;
                        charset = charset2;
                    } catch (Throwable th) {
                        if (keepConsistentOnFailure || false) {
                            if (this.slist.size() > 0) {
                                for (Object v2 : this.slist.values()) {
                                    if (v2 instanceof Value) {
                                        Value vnew;
                                        vv = (Value) v2;
                                        vnew = decodeValue(vv.encodedValue);
                                        vv.charset = vnew.charset;
                                        vv.value = vnew.value;
                                    }
                                }
                                this.list.putAll(this.slist);
                            }
                            this.multisegmentNames.clear();
                            this.slist.clear();
                        }
                    }
                } else {
                    str = (String) v2;
                    charset2 = charset;
                }
                sb.append(str);
                this.slist.remove(sname);
                segment++;
                charset = charset2;
            }
            if (segment == 0) {
                this.list.remove(name);
            } else {
                mv.value = sb.toString();
                this.list.put(name, mv);
            }
        }
        if (keepConsistentOnFailure || true) {
            if (this.slist.size() > 0) {
                for (Object v22 : this.slist.values()) {
                    if (v22 instanceof Value) {
                        vv = (Value) v22;
                        vnew = decodeValue(vv.encodedValue);
                        vv.charset = vnew.charset;
                        vv.value = vnew.value;
                    }
                }
                this.list.putAll(this.slist);
            }
            this.multisegmentNames.clear();
            this.slist.clear();
        }
    }

    public String get(String name) {
        Object v = this.list.get(name.trim().toLowerCase(Locale.ENGLISH));
        if (v instanceof MultiValue) {
            return ((MultiValue) v).value;
        }
        if (v instanceof Value) {
            return ((Value) v).value;
        }
        return (String) v;
    }

    public void set(String name, String value) {
        if (name != null || value == null || !value.equals("DONE")) {
            name = name.trim().toLowerCase(Locale.ENGLISH);
            if (decodeParameters) {
                try {
                    putEncodedName(name, value);
                    return;
                } catch (ParseException e) {
                    this.list.put(name, value);
                    return;
                }
            }
            this.list.put(name, value);
        } else if (decodeParameters && this.multisegmentNames.size() > 0) {
            try {
                combineMultisegmentNames(true);
            } catch (ParseException e2) {
            }
        }
    }

    public String toString() {
        return toString(0);
    }

    public String toString(int used) {
        ToStringBuffer sb = new ToStringBuffer(used);
        for (String name : this.list.keySet()) {
            MultiValue v = this.list.get(name);
            if (v instanceof MultiValue) {
                MultiValue vv = v;
                String ns = new StringBuilder(String.valueOf(name)).append("*").toString();
                for (int i = 0; i < vv.size(); i++) {
                    Object va = vv.get(i);
                    if (va instanceof Value) {
                        sb.addNV(new StringBuilder(String.valueOf(ns)).append(i).append("*").toString(), ((Value) va).encodedValue);
                    } else {
                        sb.addNV(new StringBuilder(String.valueOf(ns)).append(i).toString(), (String) va);
                    }
                }
            } else if (v instanceof Value) {
                sb.addNV(new StringBuilder(String.valueOf(name)).append("*").toString(), ((Value) v).encodedValue);
            } else {
                sb.addNV(name, (String) v);
            }
        }
        return sb.toString();
    }

    private static String quote(String value) {
        return MimeUtility.quote(value, "()<>@,;:\\\"\t []/?=");
    }

    private static Value decodeValue(String value) throws ParseException {
        Value v = new Value();
        v.encodedValue = value;
        v.value = value;
        try {
            int i = value.indexOf(39);
            if (i > 0) {
                String charset = value.substring(0, i);
                int li = value.indexOf(39, i + 1);
                if (li >= 0) {
                    String lang = value.substring(i + 1, li);
                    value = value.substring(li + 1);
                    v.charset = charset;
                    v.value = decodeBytes(value, charset);
                } else if (decodeParametersStrict) {
                    throw new ParseException("Missing language in encoded value: " + value);
                }
            } else if (decodeParametersStrict) {
                throw new ParseException("Missing charset in encoded value: " + value);
            }
        } catch (NumberFormatException nex) {
            if (decodeParametersStrict) {
                throw new ParseException(nex.toString());
            }
        } catch (UnsupportedEncodingException uex) {
            if (decodeParametersStrict) {
                throw new ParseException(uex.toString());
            }
        } catch (StringIndexOutOfBoundsException ex) {
            if (decodeParametersStrict) {
                throw new ParseException(ex.toString());
            }
        }
        return v;
    }

    private static String decodeBytes(String value, String charset) throws UnsupportedEncodingException {
        byte[] b = new byte[value.length()];
        int i = 0;
        int bi = 0;
        while (i < value.length()) {
            char c = value.charAt(i);
            if (c == '%') {
                c = (char) Integer.parseInt(value.substring(i + 1, i + 3), 16);
                i += 2;
            }
            int bi2 = bi + 1;
            b[bi] = (byte) c;
            i++;
            bi = bi2;
        }
        return new String(b, 0, bi, MimeUtility.javaCharset(charset));
    }
}
